package com.learn._02notice.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn._02notice.base.response.JsonResult;
import com.learn._02notice.base.response.StatusCode;
import com.learn._02notice.mapper.NoticeMapper;
import com.learn._02notice.pojo.dto.NoticeAddParam;
import com.learn._02notice.pojo.dto.NoticeListQuery;
import com.learn._02notice.pojo.dto.NoticeUpdateParam;
import com.learn._02notice.pojo.entity.Notice;
import com.learn._02notice.pojo.vo.NoticeDetailInfoVO;
import com.learn._02notice.pojo.vo.NoticeListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/*
* RestController注解：
* 组合注解，等价于Controller注解+ResponseBody注解
* 加了该注解后，所有的控制器方法上无需再添加@ResponseBody注解
* */

/*
* knife4j注解
* 1. Api（）注解：控制器类名在knife4j中显示名称
* 2. ApiOperation()注解：控制器方法名在knife4j中显示名称
* 3. ApiOperationSupport（order = 10） 注解：控制器方法在knife4j文档中的排序顺序
* 4. ApiModelProperty(value="",required=true,example="")注解：POJO类属性描述
* 5. ApiImplicitParam(name="参数名" value=“”,required=true,example="",datatype="数据类型")注解：声明参数方式接收的单个参数的描述
* 6. ApiImplicitParams({
*   @ApiImplicitParam(name="参数名" value=“”,required=true,example="",datatype="数据类型")
*   @ApiImplicitParam(name="参数名" value=“”,required=true,example="",datatype="数据类型")
* })注解：声明参数方式接收的多个参数的描述
* */
//@Controller
@RestController
@Slf4j// 日志注解 log.debug...........
@Api(tags = "01.通知管理模块") //knife4j注解
@RequestMapping("/v1/notice/")
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;


    /*
    * 1.新增公告功能
    * */
    @ApiOperationSupport(order = 10)
    @ApiOperation("新增公告")
    @PostMapping("add")
//    @ResponseBody
    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        /*
        * 梳理业务逻辑，确定接口;
        * 1.接收客户端传递来的数据
        * 2.校验数据的合法性；------------>暂时不校验
        * 3.调用mapper层插入数据[insert接口]
        * 4.返回响应
        * */
        log.debug("noticeAddParam:"+noticeAddParam);

        Notice notice = new Notice();
        /*notice.setTitle(noticeAddParam.getTitle());
        notice.setContent(noticeAddParam.getContent());
        notice.setType(noticeAddParam.getType());
        notice.setStatus(noticeAddParam.getStatus());*///太麻烦了
        BeanUtils.copyProperties(noticeAddParam,notice);//把noticeAddParam中的数据拷贝到notice中
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }


    /*
    * 2.公告列表功能
    * */
    @ApiOperationSupport(order = 20)
    @ApiOperation("公告列表")
    @GetMapping("list")
//    @ResponseBody
    public JsonResult noticeList(/*@ApiIgnore 注解：忽略这个参数     */NoticeListQuery noticeListQuery){
//        System.out.println("noticeListQuery:"+noticeListQuery);
        log.debug("noticeListQuery:"+noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();

        List<NoticeListVO> noticeListVOS = noticeMapper.selectNotice(title, type);

//        return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeListVOS);
        return JsonResult.ok(noticeListVOS);
    }

    /*
    * 3.公告详情功能
    * */
    @ApiOperationSupport(order = 30)
    @ApiOperation("公告详情")
    @GetMapping("detail")
//    @ResponseBody
    public JsonResult noticeDetailInfoVO(Long id){
//        System.out.println("id:"+id);
        log.debug("id:"+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeDetailInfo(id);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS, noticeDetailInfoVO);
        return JsonResult.ok(noticeDetailInfoVO);
    }

    /*
    * 4.删除公告功能
    * */
    @ApiOperationSupport(order = 40)
    @ApiOperation("删除公告")
    @PostMapping("del")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "通知编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "通知标题", required = true, dataType = "String", paramType = "query")
    })
//    @ApiImplicitParam(name = "id", value = "通知编号", required = true, dataType = "Long", paramType = "query")
//    @ApiImplicitParam(name = "title", value = "通知标题", required = true, dataType = "String", paramType = "query")
//    @ResponseBody
    public JsonResult delNotice(Long id,String title){
        //title 只是为了做knife4j的测试，没有任何作用
//        System.out.println("id:"+id);
        log.debug("id:"+id);
        noticeMapper.deleteNotice(id);
//        return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }

    /*
    *5.更新公告功能
    * */
    @ApiOperationSupport(order = 50)
    @ApiOperation("更新公告")
    @PostMapping("update")
//    @ResponseBody
    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam){
//        System.out.println("noticeUpdateParam:"+noticeUpdateParam);
        log.debug("noticeUpdateParam:"+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        noticeMapper.updateNotice(notice);

//        return new JsonResult(StatusCode.OPERATION_SUCCESS, notice);
        return JsonResult.ok(notice);
    }


}
